package com.kehao.kehaoTest.io;

import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * @文件字节流进行拷贝
 */
public class CopyFile {

    /**
     * @普通流
     */
    @Test
    void test01() {

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            fileInputStream = new FileInputStream("./商品测试数据.xlsx");
            fileOutputStream = new FileOutputStream("./copy.xlsx");

            //拷贝
            int len;
            //一次拷贝的大小
            byte[] bytes = new byte[1024 * 1024 * 5];

            while ((len = fileInputStream.read(bytes)) != -1) {

                fileOutputStream.write(bytes, 0, len);

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            try {
                fileOutputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }

    /**
     * @缓冲流
     */
    @SneakyThrows
    @Test
    void test02() {

        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("./商品测试数据.xlsx"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("./copy.xlsx"));

        //拷贝
        int len;
        //一次拷贝的大小
        byte[] bytes = new byte[1024 * 1024 * 5];

        while ((len = bufferedInputStream.read(bytes)) != -1) {

            bufferedOutputStream.write(bytes, 0, len);

        }

        bufferedInputStream.close();
        bufferedOutputStream.close();

    }

}
